//
// Created by yonghu0 on 25-10-9.
//

#include <functional> //greater的头文件
#include <iostream>
#include <map>     //map的头文件
#include <utility> //pair的头文件

void print(std::map<int, int> &my_map)
{
    for (auto &elem: my_map)
    {
        std::cout << elem.first << " " << elem.second << std::endl;
    }

}

int main()
{
    std::cout << "map_test begin" << std::endl;

    std::cout << "--------------创建map----------------" << std::endl;
    // 创建递增排序的map
    std::map<int, int> my_map1;
    // 创建递减排序的map
    std::map<int, std::greater<int> > my_map3;

    std::cout << "----------------容器size--------------" << std::endl;
    std::cout << my_map1.size() << std::endl;
    std::cout << "----------------容器判空---------------" << std::endl;
    std::cout << my_map1.empty() << std::endl;

    std::cout << "----------插入元素--------------------" << std::endl;
    // 插入单个元素
    // 方法一
    my_map1[1] = 11; // 如果没有<1,11>就插入，如果有<1,11>就修改key=1的value为11
    my_map1[4] = 44;
    my_map1[3] = 33;
    my_map1[2] = 22;
    print(my_map1);
    // 方法二
    std::pair<std::map<int, int>::iterator, bool> pres = my_map1.insert(std::pair<int, int>(5, 55));
    print(my_map1);
    std::cout << pres.second << std::endl;
    pres = my_map1.insert(std::pair<int, int>(5, 555)); // 插入重复元素
    print(my_map1);
    std::cout << pres.second << std::endl;
    // 插入区间

    std::cout << "------------查找元素(判断元素是否在容器中)-----------" << std::endl;
    // 方法一
    if (my_map1.find(5) != my_map1.end())
    {
        std::cout << "元素在map中" << std::endl;
    } else
    {
        std::cout << "元素不在map中" << std::endl;
    }
    // 方法二
    if (my_map1.count(5))
    {
        std::cout << "元素在map中" << std::endl;
    } else
    {
        std::cout << "元素不在map中" << std::endl;
    }

    std::cout << "------------修改map中的元素--------------" << std::endl;
    my_map1[1] = 111;
    print(my_map1);
    std::cout << "------------删除元素-----------------------------" << std::endl;
    // 删除单个元素
    // 方法一
    my_map1.erase(3);
    print(my_map1);
    my_map1.erase(11); // 删除不存在的元素，不报错
    print(my_map1);
    // 方法二
    my_map1.erase(my_map1.begin());
    print(my_map1);
    // 删除区间
    // 用的不多，这里就不列举

    std::cout << "----其他用法(lower_bound/upper_bound/equal_range)-----" << std::endl;
    // lower_bound/upper_bound/equal_range方法
    std::map<int, int>::iterator it_low_bound = my_map1.lower_bound(4);
    std::cout << it_low_bound->first << " " << it_low_bound->second << std::endl;
    std::map<int, int>::iterator it_upper_bound = my_map1.upper_bound(4);
    std::cout << it_upper_bound->first << " " << it_upper_bound->second << std::endl;
    std::pair<std::map<int, int>::iterator, std::map<int, int>::iterator> p_equal_range;
    p_equal_range = my_map1.equal_range(4);
    std::cout << (p_equal_range.first)->first << " " << (p_equal_range.first)->second << std::endl;
    std::cout << (p_equal_range.second)->first << " " << (p_equal_range.second)->second << std::endl;

    std::cout << "------------清空容器-----------------" << std::endl;
    my_map1.clear();
    print(my_map1);

    std::cout << "map_test end" << std::endl;
    return 0;
}
